/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Game.bc;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;

/**
 *
 * @author ashish.vaidya
 */
public class Game {

    private Player creator = null;
    private HashSet<Player> players = null;
    private int wordLength = 4;
    private StringBuffer responseText = new StringBuffer("<table border=1 align=\"center\" bgcolor=\"white\" ><tr><th>Guess No.</th><th>Word</th><th>Bull</th><th>Cow</th><th>Player</th></tr>");
 //private StringBuffer responseText = new StringBuffer();
    public int getWordLength() {
        return wordLength;
    }
    private int gameID = 0;
    @EJB
    private WordFactoryLocal wordFactory;
    private String theWord = "love";
    private ArrayList<BullCow> bullsAndCows = new ArrayList<BullCow>();
    private int guessNo = 0;

    public Game(Player creator, int wordLength, int UID) {
        this.creator = creator;
        this.wordLength = wordLength;
        gameID = UID;
        players = new HashSet<Player>();
        players.add(creator);
        bullsAndCows = new ArrayList<BullCow>();
        guessNo = 0;
    }

    public void reinit(int wordLength){
        responseText = new StringBuffer("<table border=1 align=\"center\" bgcolor=\"white\" ><tr><th>Guess No.</th><th>Word</th><th>Bull</th><th>Cow</th><th>Player</th></tr>");
    this.wordLength = wordLength;
    guessNo = 0;
    theWord = call(wordLength);
    }

    public void joinGame(Player player) {
        if (!players.contains(player)) {
            players.add(player);
        }
    }

    //This method is just for testing and can be removed.
    public String call(int length) {
        try {
            return wordFactory.getWord(length);
        } catch (Exception ex) {
            return "Error Aayo";
        }
    }

    public BullCow makeGuess(String word, String playerName) {
        System.out.println("****Guess Made: " + word);
        //  System.out.println("***Guess Word***:" + word);
        guessNo++;
        word = word.toLowerCase();
        if (word.length() == theWord.length()) {
            int bull = 0;
            int cow = 0;
            char[] theWordArray = theWord.toCharArray();
            char[] guessWordArray = word.toCharArray();
            StringBuffer usedChar = new StringBuffer();
            for (int i = 0; i < wordLength; i++) {
                // calculate bull
                if (theWordArray[i] == guessWordArray[i]) {
                    bull++;
                }
                if (!usedChar.toString().contains(Character.toString(guessWordArray[i]))) {
                    usedChar.append(guessWordArray[i]);
                    int charInGuess = countChar(word, guessWordArray[i]);
                    int charInWord = countChar(theWord, guessWordArray[i]);
                    if (charInGuess <= charInWord) {
                        cow = cow + charInGuess;
                    } else {
                        cow = cow + charInWord;
                    }
                }
            }
            cow = cow - bull;
            BullCow bc = new BullCow(bull, cow);
            bullsAndCows.add(bc);
            updateGuesses(bc,playerName, word);
            return bc;
        } else {
            return new BullCow(9, 9);
        }
    }

    private int countChar(String word, char c) {
        int count = 0;
        char[] wordArray = word.toCharArray();
        for (int i = 0; i < word.length(); i++) {
            if (wordArray[i] == c) {
                count++;
            }
        }
        return count;
    }

    public String getTheWord() {
        return theWord;
    }

    public String getOutput() {
        return responseText.toString();
    }

    private void updateGuesses(BullCow bc, String player, String guessedWord) {
       responseText.append("<tr><td>" + guessNo + "</td><td> " + guessedWord + "</td><td> Bull : " + bc.getBull() + "</td><td>  Cow : " + bc.getCow() +"</td><td> "+player+" "+ "</td></tr> ");
       // responseText.append("(" + guessNo + ") " + guessedWord + ":  Bull : " + bc.getBull() + "  Cow : " + bc.getCow() +" ["+player+"] "+ "<br/>");
        if (bc.getBull() == wordLength){
        responseText.append(""+player.toUpperCase()+" HAS WON THE GAME! THE WORD WAS :" + theWord);
        }
    }

    public void setTheWord(String theWord) {
        this.theWord = theWord;
    }

    public Player getCreator() {
        return creator;
    }
}
